#! /usr/bin/env bash

REMOTE_REPO=$1
LOCAL_WORKSPACE=$2
REF_REPO=$3

if [ -z $REMOTE_REPO -o -z $LOCAL_WORKSPACE -o -z $REF_REPO ]; then
    echo "invalid call pull-repo-ref.sh '$REMOTE_REPO' '$LOCAL_WORKSPACE' '$REF_REPO'"
elif [ ! -d $LOCAL_WORKSPACE ]; then
    git clone --reference $REF_REPO $REMOTE_REPO $LOCAL_WORKSPACE
    cd $LOCAL_WORKSPACE
    git repack -a
    cd -
else
    cd $LOCAL_WORKSPACE
    git checkout . && git clean -xfd && git checkout main
    git fetch --all --tags -f
    cd -
fi